<?php
//this model is using for users....
class User_model extends Model{
	
	function User_model()
	{
		parent::Model();
		$this->load->database();
	}
	
	function change_pwd()
	{
		$user_ses = $this->session->userdata('userlogindetails');
		
		$this->db->query("update tbl_users set password='".md5($_POST['pwd'])."' where user_id=".$user_ses['user_id']);
	}
	
  function validate_user()
  {
    $result = $this->db->query("select *,count(user_id) as ct from tbl_users where user_email='".$_POST['username']."' and password='".md5($_POST['password'])."'");
    $checkdata=$result->result();
    $check=$checkdata[0];
    
    if($check->ct==1)
    {
      $userArray=array();
      $userArray['user_id']=$check->user_id;
      $userArray['user_name']=$check->user_name;
      $userArray['user_group']=$check->group_id;
      $userArray['loggedat']=time();
      $this->session->set_userdata('userlogindetails',$userArray);
      $this->session->unset_userdata('invalid');  
       if($check->group_id == 1)
      {
          redirect(base_url().'idea_home/admin_dashboard');
      } 
      else
      {
          redirect(base_url().'idea_home/general_dashboard');
      }
      
    }
    else
    {
      $this->session->set_userdata('invalid','Please check username and password');
      redirect(base_url().'login');
    }
  }
  
  //to send users details to flexigrid...
  public function get_users() 
  {
    $this->CI =& get_instance();
    //Select table name
        
    //Build contents query
//    $this->db->query()
    $this->db->select('* from tbl_users ');
    $this->CI->flexigrid->build_query();
    
    //Get contents
    $return['records'] = $this->db->get();
    
    //Build count query
    $this->db->select('count(user_id) as record_count from tbl_users');
    $this->CI->flexigrid->build_query(FALSE);
    $record_count = $this->db->get();
    $row = $record_count->row();
    
    //Get Record Count
    $return['record_count'] = $row->record_count;
  
    //Return all
    return $return;
  }
  
  /*
	//to count number of users...
	function users_count()
	{
		$result = $this->db->query("select * from tbl_users ");
		return $result->num_rows();
	}
	
	// to list user info....
	
	function list_users($num,$limitnum)
	{
		$result = $this->db->query("select * from tbl_users order by user_name ASC limit ".$num." , ".$limitnum);
		return $result;
	}
	*/
	
	//to add new user...
	function insert_user()
	{
	 // print_r($_POST);exit;
	  $name = trim($this->input->post("name"));
    $email = trim($this->input->post("email"));
    $loc = trim($this->input->post("loc_addr"));
    $per = trim($this->input->post("per_addr"));
    $phone = trim($this->input->post("phone"));
    $city = trim($this->input->post("city"));
    $landline = trim($this->input->post("landline"));
		$check = $this->db->query("select * from tbl_users where user_email like '".$_POST['email']."'");
		
		if($check->num_rows() == 0)
		{
			
			$this->db->set('user_name', $name);
			$this->db->set('user_add1', $loc);
			$this->db->set('user_add2', $per);
			$this->db->set('user_city', $city);
			$this->db->set('user_phone', $phone);
			$this->db->set('user_email', $email);
			$this->db->set('user_number', $landline);
			$this->db->set('password',md5($email));

			$this->db->insert('tbl_users');
      return 1;	
		}
		else
		{
			return 0;
		}
	}
	
	//to fetch user details.....
	function fetch_user($id=0)
	{	
		
		$result = $this->db->query("select a.*,b.* from tbl_users as a,tbl_city as b where a.user_id ='".$id."' and b.city_id = a.user_city" );
		return $result;
	}
	//to fetch city name...
	function fetch_city()
	{
		$result = $this->db->query("select * from tbl_city order by city_name asc");
		return $result;
	}
	//to save edit...
	function save_edit()
	{
		print_r($_POST);
		
		$data = array(
               'user_name' => $_POST['user_name'], 'user_phone' => $_POST['phone'], 'user_number' => $_POST['landline'], 'user_city' => $_POST['city'], 'user_add1' => $_POST['addr1'],
                'user_add2' => $_POST['addr2'],'user_email'=> $_POST['email']);
                $this->db->where('user_id', $_POST['user_id']);
		$this->db->update('tbl_users', $data); 
	}
	
		
	
}
